<?php
require 'sphinxapi.php';
/**
 * Template Name: sphinxtest
 *
 * 通用的footer
 * date:2011/4/11 
 * @author Ryan 
 * 
 */
//全局参数()；
	$pageSize = 10 ;
	$pageModel= 10 ; //不允许改变的值
	$selfPageLink = "/" ; //自身链接；
	$autoLink = "" ;
	$siteurl = get_bloginfo('template_url');
	$keyword = $_GET['keyword'] ;
	//父类型id
	$cat = $_POST['cat'] ; 
	if($cat == null || "" == $cat){
		$cat = $_GET['cat'] ;
	}
	$categories = "" ;
	$catStr = "" ;
	if($cat != null && "" != $cat){
		$category_id = get_cat_ID($cat);
		$categories = get_categories ('child_of='.$category_id) ;
//		$categories = $wpdb->get_results("select p.name,p.term_id as  pterm_id ,t.name,t.term_id as term_id ".
//			" from wp_terms t ,wp_term_taxonomy m , wp_terms p ".
//			" where t.term_id = m.term_id and p.term_id = m.parent and p.term_id  ='".$category_id."'") ;
		foreach ($categories as $category) {
			$catStr =$catStr."'".$category->term_id."'," ;
		}
	}
	if($keyword!= null){
		$keyword =str_replace('\\\'',"&#39;",$keyword) ;
		$keyword =str_replace('\\\"',"&#34;",$keyword) ;
		$oldKeyword = $keyword ;
		$oldKeyword = str_replace("&#34;",'"',$keyword );
		$oldKeyword = str_replace("&#39;","'",$oldKeyword );
		$keyword = str_replace($endSpanChar,"",$keyword)  ;
		$keyword= trim($keyword)  ;
	}
	$oldKeywordArray = explode(" ",$keyword);
	$keywordArray = array() ;
	//页数
	$paged = $_POST["paged"]; 
	if($paged == null || "" == $paged ){
		$paged = $_GET["paged"]; 
		if($paged == null || "" == $paged ){
			$paged = 0 ;
		}
	}
	//这里首先查询出所有类型
	$queryStr = "select distinct terp.term_id as term_id, terp.name as name from wp_term_taxonomy tax ,wp_terms terp where tax.parent = terp.term_id" ;
	$cats = $wpdb->get_results($queryStr );
	
	$statNum = $pageSize * $paged ;
	
	
	function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)    {
        if(function_exists("mb_substr")){
			if(mb_strlen($str, $charset) <= $length) return $str;
			$slice = mb_substr($str, $start, $length, $charset);
		}else{

            $re['utf-8']  = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";

            $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";

            $re['gbk']     = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";

            $re['big5']     = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";

            preg_match_all($re[$charset], $str, $match);

            if(count($match[0]) <= $length) return $str;
            $slice = join("",array_slice($match[0], $start, $length));
        }
        if($suffix) return $slice."…";
        return $slice;

    }
	
$siteurl = get_bloginfo('template_url'); 

$cl = new SphinxClient ();
$cl->SetServer ( 'localhost', 9312);
$cl->SetConnectTimeout ( 3 );
$cl->SetArrayResult ( true );
$cl->SetMatchMode ( SPH_MATCH_ANY);
$cl->SetLimits ( $statNum, $pageSize, 1000 );
$cl->SetFieldWeights(array("post_title"=>100, "post_content"=>1));
//$cl->SetSortMode ( SPH_SORT_ATTR_ASC, "gamepi" );
$res = $cl->Query ( $keyword, "*" );



get_header();

?>
<script type="text/javascript">

$(document).ready(function (){

	$("#goser").click(function (){
			var key = $("#test").val();
			window.location.href="/?page_id=118326&key="+key;
		});

	
});

</script>
<div id="list_left">
				<ul>
					<li id="xuanzhong">>快速分类</li>
					<?php 
					
						if($category_id != null && "" != $category_id){
					?>
							<li class="fenleileftfloat NOxuanzhong" ><a href="<?php echo $selfPageLink."?s=".$oldKeyword  ;?>">全部游戏</a></li>
					<?php 
						}else{
					?>
							<li id="xuanzhong" class="fenleileftfloat"><a href="<?php echo $selfPageLink ;?>">全部游戏</a></li>
					<?php 
						}
					?>
					<?php 
						foreach($cats as $v){
							if($v->term_id == $category_id){
					?>
								<li id="xuanzhong" class="fenleileftfloat"><a href="<?php echo $selfPageLink."?s=".$oldKeyword."&cat=".$v->name;?>" ><?php echo $v->name; ?></a></li>
					<?php 	
							}else{
					?>
								<li class="fenleileftfloat NOxuanzhong"><a href="<?php echo $selfPageLink."?s=".$oldKeyword."&cat=".$v->name;?>"><?php echo $v->name; ?></a></li>
					<?php 			
							}
						}
					?>
					<!-- 
					<li class="NOxuanzhong"><a href="#">网页游戏</a></li>
					<li class="NOxuanzhong"><a href="#">flash游戏</a></li>
					<li class="NOxuanzhong"><a href="#">手机游戏</a></li> -->
				</ul>
			</div>
			<div id="list_center">
				<!--  搜索结果列表开始 -->
				<?php 
//					$postTable = "(select p.* ,m.meta_value meta_value from wp_posts p left join wp_postmeta  m on  m.post_id =  p.ID and m.meta_key='GamePI' and p.post_status = 'publish' AND p.post_type = 'post' )" ;
//					$condition = " where post.ID = ships.object_id and ships.term_taxonomy_id = tax.term_taxonomy_id   and tax.taxonomy ='category'  " ;
//					foreach ($oldKeywordArray as $value){
//						if($value!= null && $value!=""){
//							$keywordArray[$value] = $value ;
//							$condition=$condition." and post.post_title like '%".$value."%' ";
//						}
//					}
//					
////					foreach ($oldKeywordArray as $value){
////						if($value!= null && $value!=""){
////							$keywordArray[$value] = $value ;
////							$condition=$condition." or post_content like '%".$value."%' ";
////						}
////					}
////					if($keyword != null && "" != $keyword){
////						$condition=$condition." and post_title like '%".$keyword."%' ";
////					}
//					if($category_id != null && "" != $category_id){
//						if($catStr !=null && "" != $catStr){
//							$catStr = substr($catStr,0,strlen($catStr)-1) ;
//							$condition=$condition." and tax.term_id in (".$catStr.") " ;
//						}else{
//							$condition=$condition." and tax.term_id = '' " ;
//						}
//					}
//					$postCountQue = "select count(distinct post.post_title) as counts from  wp_term_taxonomy tax ,wp_term_relationships ships, ".$postTable." post ".$condition ;
//					$mycounts = $wpdb->get_results($postCountQue) ;
//					$mycount= 0 ;
//					if($mycounts != null){
//						$mycount = $mycounts[0]->counts ;
//					}
					$postids = '';
					foreach($res['matches'] as $postidsArray){
						$postids .= $postidsArray['id'].",";
					}

					$postids = substr($postids, 0, strlen($postids)-2);
				?>
				<span id="mianbaoxie" class="list_centtopspan">当前位置：<a href="?">首页</a>=><a href="<?php echo $selfPageLink ;?>">游戏搜索</a></span>
				<span id="soso_result" class="list_centtopspan">找到<?php echo $res['total_found']; ?></>个相关游戏     搜索用时:".$res['time']."  当前展示前20条</span>
				<?php 
					//$args = array ('paged'=>$paged,"orderby"=>"date");	
					//query_posts($args);
					//while (have_posts()) : the_post(); 
					
//					$postQue = "select count(distinct post.post_title) ,post.ID as ID ,post.post_title as post_title ,tax.term_id as term_id ,post.post_content as post_content ".
//						"from wp_term_taxonomy tax ,wp_term_relationships ships, ".$postTable." post ".$condition." group by post.post_title  ORDER BY post.meta_value DESC LIMIT ".$statNum.",".$pageSize ;
//					
					$postQue =  "select ID,post_title, post_content from wp_posts where ID in (".$postids.")";
					$myposts = $wpdb->get_results($postQue);
					$startSapn = "<span style='color:red;'>";
					$endSpan = "</span>" ;
					foreach($myposts as $mypost) {
						// get game image by user defined
						//$gameSize = get_post_meta($mypost->ID, 'GameSize', true);  
						$gamePic = get_post_meta($mypost->ID, 'GamePic', true);  
						$gameLink = get_post_meta($mypost->ID, 'GameLink', true);  
						$postLink = get_permalink($mypost->ID);
						
						$postTitle = $mypost->post_title ; 
						$postTitle = str_replace("%","",$postTitle) ;
						$postTitle = str_replace("&quot;","&#39;",$postTitle) ;
						$titleHtml = $postTitle ;
						foreach($keywordArray as $value) {
							if($value !=null && $value != ""){
								$startFindPos = 0 ;
								$titleHtml = "" ;
								$indexPos = stripos($postTitle,$value,$startFindPos) ;
								if($indexPos >-1){
									do{
										$titleHtml = $titleHtml.substr($postTitle,$startFindPos,$indexPos-$startFindPos).$startSapnChar.
											substr($postTitle,$indexPos,strlen($value)).$endSpanChar.substr($postTitle,$indexPos+strlen($value)) ;
										$startFindPos = $indexPos +  strlen($value);
											
									} while(($indexPos = stripos($postTitle,$value,$startFindPos)) >-1) ;
								}else{
									$titleHtml = $postTitle ;
								} 
								
//								$titleArray = explode($value,$postTitle) ;
//								$titleHtml = "" ;
//								if(count($titleArray)>0){
//									$arrayCount = count($titleArray) ;
//									for($i= 0 ;$i<$arrayCount -1;$i++){
//										$titleHtml = $titleHtml.$titleArray[$i].$startSapnChar.$value.$endSpanChar ;
//									}
//									$titleHtml = $titleHtml.$titleArray[$arrayCount -1] ;
//								} else {
//									$titleHtml = $postTitle ;
//								}
								$postTitle = $titleHtml ;
							}
							
						}
						$titleHtml = str_replace($startSapnChar,$startSapn,$titleHtml) ;
						$titleHtml = str_replace($endSpanChar,$endSpan,$titleHtml) ;
						
						//mb_strimwidth(strip_tags(apply_filters('the_content', $mypost->post_content)), 0, 300,"…");
						$desHtml = csubstr($mypost->post_content, 0, 300, "utf-8", true)  ;
						
						
				?>
						<div class="list">
							<span class="list_img"><a href="<?php echo $postLink; ?>"><img src="<?php echo $gamePic ?>" width="104" height="79" alt="<?php echo $titleHtml; ?>" title="<?php echo $titleHtml; ?>" /></a></span>
							<h3 class="list_title"><a href="<?php echo $postLink; ?>"><?php echo $titleHtml;//$mypost->post_title; ?></a></h3>
							<span class="list_cat">							
				         <?php 
//				           $cat_name = get_cat_name($mypost->term_id);
						$cat_name = "Flash小游戏";
				           if($cat_name == 'andriod' ||$cat_name == 'iphone' || $cat_name == 'windows phone' ){
				           	echo "游戏平台:";
				           }else {
				           	echo "类别:";
				           }
							?>
							<samp><?php echo $cat_name?></samp></span>
							<p class="list_neirong"><?php echo $desHtml; ?></p>
							<span class="list_add">游戏源地址：<!-- <a href="<?php //echo $gameLink; ?>"> --><samp><?php echo $gameLink; ?></samp><!-- </a> --></span>
						</div>
				<?php 
					}
					//endwhile;
				?>
				<!--  搜索结果列表结束 -->
				
				<!--  分页开始 -->
				
				<div id="scroll">
					<?php 
					$totalPage = ceil($res['total_found']/$pageSize) ;
					if($totalPage >0){
						$i = 0 ;
						$pagedLink = $selfPageLink."?s=".$oldKeyword."&cat=".$cat;
						if(intval($paged) > 0 ){		
					?>
							<a href="<?php echo $pagedLink."&paged=".(intval($paged)-1); ?>" ><img src="<?php echo $siteurl ?>/images/next-top.gif" alt="上一页" title="上一页" class="f_l"/></a>
					<?php 
						}
					?>
					<?php 
						if($totalPage<=$pageModel){
						 	for(;$i<$totalPage;$i++){
						 		if($i == intval($paged)){
					?>
									<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
						 		}else{
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
						 		}
						 	}
						 		
						}elseif($totalPage < $pageModel + 3){
							$halfPageModel = intval($pageModel /3)  ;
							if(intval($paged) < $halfPageModel*2){
								
								for($i=0;$i < $halfPageModel*2+1;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 		
								for($i = $totalPage-($pageModel-$halfPageModel*2 -2);$i<$totalPage;$i++){
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 				
								}	
					?>
					<?php 
							}elseif(intval($paged) > $halfPageModel - 1) {
								for($i =0 ;$i< $halfPageModel-1;$i++){
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 				
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 	
								for($i=$totalPage-($pageModel-$halfPageModel);$i < $totalPage;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}
							}
						}else{
							$halfPageModel = intval($pageModel /2)  ;
							if(intval($paged) < $halfPageModel-1){
								for($i =0 ;$i< $halfPageModel;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 			
								for($i =$totalPage - ($pageModel - $halfPageModel - 1) ;$i< $totalPage ;$i++){
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php				
								}
							}elseif(intval($paged) > $totalPage - ($halfPageModel) ) {
								for($i = 0 ;$i< $halfPageModel-1 ;$i++){
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php				
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 		
								for($i = $totalPage - ($pageModel - $halfPageModel) ;$i< $totalPage;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}	
								
							}else {
					?>
								<a href="<?php echo $pagedLink."&paged=0"; ?>" class="linkstyle03"><?php echo 1;?></a>
								<a href="<?php echo $pagedLink."&paged=1"; ?>" class="linkstyle03"><?php echo 2;?></a>
					<?php 
								$uses = 3 ;
								if($paged > $totalPage/2){
									$uses = 2 ;
					?>
									<a href="<?php echo $pagedLink."&paged=1"; ?>" class="linkstyle03"><?php echo 3;?></a>
					<?php				
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
								<a href="<?php echo $pagedLink."&paged=".(intval($paged)-1); ?>" class="linkstyle03"><?php echo intval($paged);?></a>
								<font class="linknone"><?php echo (intval($paged) +1);?></font>
								<a href="<?php echo $pagedLink."&paged=".(intval($paged)+1); ?>" class="linkstyle03"><?php echo (intval($paged) +2);?></a>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 		
								for($i = $totalPage - $uses ;$i< $totalPage;$i++){
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
								}
							}
						}						
						if(intval($paged)<$totalPage-1){
					?>
							<a href="<?php echo $pagedLink."&paged=".(intval($paged)+1); ?>" ><img src="<?php echo $siteurl ?>/images/next-bottom.gif" alt="下一页" title="下一页" class="f_l" /></a>
					<?php 
						}
					}
					?>
				</div>
				
				<!--  分页结束 -->
				
				
			</div>
			
			<!-- 热门游戏开始 -->
			<div id="list_right">
				<h2>热门游戏推荐</h2>
				<?php 
//					$hotPostQue = "select count(post.post_title) ,post.ID as ID ,post.post_title as post_title ,tax.term_id as term_id ".
//						" from wp_term_taxonomy tax ,wp_term_relationships ships, wp_posts post where ".
//						" post.ID = ships.object_id and ships.term_taxonomy_id = tax.term_taxonomy_id and post_status = 'publish' AND post_type = 'post'  and tax.taxonomy ='category' ".
//						"  group by post.post_title  order by post.comment_count desc limit 0,10 ";
					//query_posts('orderby=comment_count&showposts=4');//返回分类ID为7的2篇内容
					//while (have_posts()) : the_post(); 
					$i =1 ;
					$hotposts = getHotPost(10); // $wpdb->get_results($hotPostQue);
					foreach($hotposts as $hotpost) {
						//$gameSize = get_post_meta($hotpost->ID, 'GameSize', true);  
						$gamePic = get_post_meta($hotpost->ID, 'GamePic', true);  
						//$gameLink = get_post_meta($hotpost->ID, 'GameLink', true);  
						$postLink = get_permalink($hotpost->ID);
				?>
						<div class="list_tuijian">
							<span class="list_img"><a href="<?php echo $postLink; ?>"><img src="<?php echo $gamePic; ?>" width="104" height="79" alt="<?php echo $hotpost->post_title;  ?>" title="<?php echo $hotpost->post_title;?>" /></a></span>
							<!-- <span class="listriht listriht_tuijian"><img src="<?php echo $siteurl ?>/images/tuijian/<?php echo  $i++ ;?>.jpg" /></span> -->
							<h5 class="listriht listriht_tit"><a href="<?php echo $postLink; ?>"><?php echo $hotpost->post_title;  ?></a></h5>
							<span class="listriht listriht_cat "><?php echo get_cat_name($hotpost->term_id);?></span>
						</div>
				<?php 
					}
					//endwhile;
				?>
			</div>
			<!-- 热门游戏结束 -->
		</div>
		<?php get_footer();?>